﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtension
{
    public static class LambdaComparer
    {
        public static IEqualityComparer<TSource> Create<TSource, TResult>(Func<TSource, TResult> selector)
        {
            if (selector == null)
                throw new ArgumentNullException("selector", "selector is null.");

            return new InternalComparer<TSource, TResult>(selector);
        }

        private class InternalComparer<TSource, TResult> : IEqualityComparer<TSource>
        {
            private readonly Func<TSource, TResult> selector;

            public InternalComparer(Func<TSource, TResult> selector)
            {
                this.selector = selector;
            }

            public bool Equals(TSource x, TSource y)
            {
                TResult result1 = selector(x);
                TResult result2 = selector(y);
                return result1.Equals(result2);
            }

            public int GetHashCode(TSource obj)
            {
                TResult result = selector(obj);
                return result.GetHashCode();
            }
        }


    }
}
